package edu.mtu.citizenscience.mushroommapper.activity.main;

import edu.mtu.citizenscience.mushroommapper.activity.R;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Progress dialog for displaying a progress bar and message
 * it defaults to displaying an indeterminate progress bar until
 * progress max is set. 
 * 
 * It can be set back to indeterminate by
 * setting progress max to 0
 */
public class ProgressDialog extends DialogFragment {
	// the max value for the progress bar
	private int max = 0;
	// the progress label for the dialog
	private String label = "";
	// the dialog title
	private String title = "Progress";
	
	// whether the dialog is attached or not
	private boolean attached = false;
	// the dialogs progress bar
	private ProgressBar progressBar;
	// the dialogs progress message text view
	private TextView updateText;
	// the dialogs title text view
	private TextView titleText;
	
	/**
	 * Progress Dialog creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        
        // remove the title bar from this dialog
        this.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }
	
	/**
	 * Create view for the Progress Dialog
	 * 
	 * @param inflater - LayoutInflater - for inflating views
	 * @param container - ViewGroup - the dialogs view container
	 * @param savedInstanceState - Bundle - Bundle with launch info
	 * @return View - the View for the fragment's UI, or null
	 */
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
		// inflate the progress dialog view
        View view = inflater.inflate(R.layout.dialog_progress, container, false);
        
        // get the progress bar from the dialog view
        this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        // get the update text view from the dialog view
        this.updateText = (TextView) view.findViewById(R.id.updateStatus);
        // get the title text view from the dialog view
        this.titleText = (TextView) view.findViewById(R.id.progressTitle);
        
        // set the progress bar as indeterminate
        this.progressBar.setIndeterminate(true);
        
        // return the inflated view
        return view;
    }
	
	/**
	 * Show the progress dialog if it is not already attached
	 * 
	 * @param manager - FragmentManager - The FragmentManager this fragment will be added to.
	 * @param tag - String - The tag for this fragment, as per FragmentTransaction.add.
	 * @return void
	 */
	@Override
	public void show(FragmentManager manager, String tag)
	{
		// check that the dialog is not already attached
		if ( ! this.attached)
		{
			// set that this dialog is attached
			this.attached = true;
			
			// show the dialog
			super.show(manager, tag);
		}
	}
	
	/**
	 * Dismiss the progress dialog if it is attached
	 * 
	 * @param void
	 * @return void
	 */
	@Override
	public void dismiss()
	{
		// check to make sure the dialog is attached
		if (this.attached)
		{
			// set attached to false
			this.attached = false;
			
			// reset the max to 0
			this.setMax(0);
			// reset the current progress to 0
			this.updateProgress(0);
			// reset the title to Progress
			this.setTitle("Progress");
			// reset the progress label to empty
			this.setProgressLabel("");
			
			// dismiss the dialog
			super.dismiss();
		}
	}
	
	/**
	 * Sets the max value for the dialog progress bar
	 * 
	 * @param max - int - the max for the progress bar
	 * @return void
	 */
	public void setMax(int max)
	{
		// set the progress max
		this.max = max;
	}
	
	/**
	 * Updates the current progress bar value
	 * 
	 * @param progress - int - the value for the progress bar
	 * @return void
	 */
	public void updateProgress(int progress)
	{
		// make sure progress dialog was created
		if (this.progressBar != null && this.updateText != null)
		{
			// check if we have max progress
			if (this.max > 0)
			{
				// we have max so the progress bar is determinate
				this.progressBar.setIndeterminate(false);
				// set the max value on the progress bar
				this.progressBar.setMax(this.max);
				// set the current value on the progress bar
				this.progressBar.setProgress(progress);
				// set the progress text as the current progress over the max progress plus the progress label
				this.updateText.setText(progress + "/" + this.max + " " + this.label);
			}
			else
			{
				// we don't have a max so set the progress bar as indeterminate
				this.progressBar.setIndeterminate(true);
				// set the progress text as the progress label
				this.updateText.setText(this.label);
			}
				
			// set the progress dialog title text
			this.titleText.setText(this.title);
		}
	}
	
	/**
	 * Sets the title for the progress dialog
	 * 
	 * @param title - String - the title for the progress dialog
	 * @return void
	 */
	public void setTitle(String title)
	{
		// set the title for this dialog
		this.title = title;
	}
	
	/**
	 * Sets the progress label for the progress dialog
	 * 
	 * @param label - String - the progress label for the progress dialog
	 * @return void
	 */
	public void setProgressLabel(String label)
	{
		// set the label for this dialog
		this.label = label;
	}

}
